package com.dongdongshop.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.data.Result;
import com.dongdongshop.domain.model.TbContent;
import com.dongdongshop.domain.model.TbContentExample;
import com.dongdongshop.domain.vo.ContentVO;
import com.dongdongshop.mapper.TbContentMapper;
import com.dongdongshop.service.ContentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@Service
public class ContentServiceImpl implements ContentService {

    @Autowired
    private TbContentMapper contentMapper;

    @Autowired
    private StringRedisTemplate template;

    @Override
    public List<ContentVO> contentList(Long cid) {
        String list = template.boundValueOps("contents:" + cid).get();
        if (list != null){//如果redis中有值
            return (List<ContentVO>) JSONObject.parse(list);
        }
        if (cid == 0){
            List<TbContent> tbContentList = contentMapper.selectByExample(null);
            List<ContentVO> collect = tbContentList.stream().map(tbContent -> {
                ContentVO contentVO = new ContentVO();
                BeanUtils.copyProperties(tbContent, contentVO);
                return contentVO;
            }).collect(Collectors.toList());
            template.opsForValue().set("contents:" + 0, JSONObject.toJSONString(collect));
            return collect;
        }
        TbContentExample example = new TbContentExample();
        example.createCriteria().andCategoryIdEqualTo(cid);
        List<TbContent> tbContentList = contentMapper.selectByExample(example);
        List<ContentVO> voList = tbContentList.stream().map(tbContent -> {
            ContentVO contentVO = new ContentVO();
            BeanUtils.copyProperties(tbContent, contentVO);
            return contentVO;
        }).collect(Collectors.toList());
        template.opsForValue().set("contents:" + cid, JSONObject.toJSONString(voList));
        return voList;
    }

    @Override
    public Result addContent(ContentVO contentVO) {
        Set<String> keys = template.keys("contents:"+"*");
        template.delete(keys);
        TbContent content = new TbContent();
        BeanUtils.copyProperties(contentVO,content);
        contentMapper.insertSelective(content);
        try{
            Thread.sleep(1000);//睡眠一秒钟
        }catch (InterruptedException e){
            log.info("延时的异常: {}",e);
        }
        template.delete(keys);
        return Result.OK();
    }

    @Override
    public ContentVO selectById(Long id) {
        TbContent content = contentMapper.selectByPrimaryKey(id);
        ContentVO contentVO = new ContentVO();
        BeanUtils.copyProperties(content,contentVO);
        return contentVO;
    }

    @Override
    public Result update(ContentVO contentVO) {
        Set<String> keys = template.keys("contents:"+"*");
        template.delete(keys);
        TbContent content = new TbContent();
        BeanUtils.copyProperties(contentVO,content);
        contentMapper.updateByPrimaryKeySelective(content);
        try{
            Thread.sleep(1000);//睡眠一秒钟
        }catch (InterruptedException e){
            log.info("延时的异常: {}",e);
        }
        template.delete(keys);
        return Result.OK();
    }

    @Override
    public Result delete(Long id) {
        Set<String> keys = template.keys("contents:"+"*");
        template.delete(keys);
        contentMapper.deleteByPrimaryKey(id);
        try{
            Thread.sleep(1000);//睡眠一秒钟
        }catch (InterruptedException e){
            log.info("延时的异常: {}",e);
        }
        template.delete(keys);
        return Result.OK();
    }

    @Override
    public Result deleteContent(List<Long> id) {
        Set<String> keys = template.keys("contents:"+"*");
        template.delete(keys);
        TbContentExample example = new TbContentExample();
        example.createCriteria().andIdIn(id);
        contentMapper.deleteByExample(example);
        try{
            Thread.sleep(1000);//睡眠一秒钟
        }catch (InterruptedException e){
            log.info("延时的异常: {}",e);
        }
        template.delete(keys);
        return Result.OK();
    }
}
